/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int getLen(ListNode* head,ListNode*& tail){

        int cnt=0;
        while(head){
            if(head->next==nullptr){
                tail=head;
            }
            ++cnt;
            head=head->next;
        }

        return cnt;
    }
    ListNode* rotateRight(ListNode* head, int k) {
        
        ListNode *tmp,*tail;
        int len=getLen(head,tail);
        if(len==0 || len==1){
            return head;
        }
        k%=len;
        tmp=head;
        tail->next=head;
        for(int i=1;i<len-k;++i){
            tmp=tmp->next;
        }
        ListNode* ret=tmp->next;
        tmp->next=nullptr;

        return ret;
    }
};
